package com.my.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.my.bean.Firesafety;
import com.my.bean.ResultPage;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.FiresafetyService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消防安全 前端控制器
 * </p>
 *
 * @author 闫亚茹
 * @since 2018-09-08
 */
@Controller
@RequestMapping("/firesafety")
public class FiresafetyController {
	@Autowired
    FiresafetyService firesafetyService;

    Logger logger=Logger.getLogger(FiresafetyController.class);
    //添加
    @RequestMapping("/addbaFiresafety")
    @ResponseBody
    public  String addbaFiresafety(String param,HttpSession session){
        Firesafety firesafety= JSON.parseObject(param,Firesafety.class);
        firesafety.setCreateTime(new Date().getTime());
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        firesafety.setAreaid(areaid);
        if (firesafetyService.insert(firesafety)){
            logger.info("{addbaFiresafety|保存成功}");
            return  "保存成功";
        }else{
            logger.info("{addbaFiresafety|保存失败}");
            return  "保存失败";
        }
    }
    /**
     * 查询并分页
     * @param voPage
     * @return
     */
    @RequestMapping("/selectFiresafety")
    @ResponseBody
    public ResultPage <Firesafety> selectFiresafety(String lin, String data, VoPage voPage, HttpSession session ){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }

        Map map=new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<Firesafety> resultPage = firesafetyService.selectMyPage(map,voPage,areaid,admin);
        return resultPage;
    }

    @RequestMapping("/Firesafety")
    public String Firesafety (){
        return "Firesafety";
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping("/firesafety_add")
    public String firesafetyadd(){ return "/Alert/Firesafety_Add";

    }

    /**
     * 跳转修改页面
     * @return
     */
    @RequestMapping("/firesafety_Edit")
    public String firesafetyEdit(){ return "/Alert/Firesafety_Edit";

    }

    @RequestMapping("/delectFiresafety")
    @ResponseBody
    public boolean delectFiresafety(Long id){
        return  firesafetyService.deleteById(id);
    }
    @RequestMapping("/updateFiresafety")
    @ResponseBody
    public String updateFiresafety(String param){
        Firesafety  firesafety = JSONObject.parseObject(param,Firesafety.class);
        if(firesafetyService.updateById(firesafety)){
            return  "修改成功";
        }else{
            return "修改失败";
        }

    }






}
